// miniprogram/pages/info/zmyygzs/zmyygzs.js
var app = getApp();
var util = require('../../../utils/util.js');
var gzry_dep = "", gzry_zqdd = "", gzry_name = "", gzryphone, frist_on = "首次登记人员",  twvalue;
var name, sfz, phone, gzry_memo_zy, laifangmudi, beifangzhekeshi, beifangzhename, select_value;
Page({  
  /**
   * 页面的初始数据
   */
  data: {
    zmyymz_fz_tw_moda: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    this.findOpenid();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../../chooseLib/chooseLib',
      })
      return
    }
 
  },

  //拍照或者从相册选择要识别的照片
  photo(type) {
    let that = this

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], ///'original',压缩图
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let imgUrl = res.tempFilePaths[0];

        that.uploadImg(app.globalData.openid + new Date().getTime(), imgUrl)

      }

    })

  },

  // 上传图片到云存储
  uploadImg(type, imgUrl) {

    let that = this
    wx.cloud.uploadFile({
      cloudPath: 'ocr/' + type + '.png',
      filePath: imgUrl, // 文件路径
      success: res => {
        // console.log("上传成功", res.fileID)
        that.getImgUrl(type, res.fileID)
      },
      fail: err => {
        // console.log("上传失败", err)
      }
    })

  },

  //获取云存储里的图片url
  getImgUrl(type, imgUrl) {
    wx.showToast({
      title: '正在识别',
      icon: 'loading'
    })
    let that = this
    wx.cloud.getTempFileURL({
      fileList: [imgUrl],
      success: res => {
        let imgUrl = res.fileList[0].tempFileURL
        // console.log("获取图片url成功", imgUrl)
        that.shibie(type, imgUrl)
      },
      fail: err => {
        // console.log("获取图片url失败", err)
      }
    })
  },

  //调用云函数，实现OCR识别
  shibie(type, imgUrl) {
    // console.log("识别成功shibie", type)
    let that = this
    wx.cloud.callFunction({
      name: "photo",
      data: {
        type: "shenfenzheng",
        imgUrl: imgUrl
      },
      success(res) {

        switch (res.result.gender) {

          case "男":
            that.setData({
              name: res.result.name,
              sfz: res.result.id,
              address: res.result.addr,
              sex_radio: [{ checked: true, anme: "男", value: "男" },
              { checked: false, anme: "女", value: "女" }]
            })
            break;
          case "女":
            that.setData({
              name: res.result.name,
              sfz: res.result.id,
              address: res.result.addr,
              sex_radio: [{ checked: false, anme: "男", value: "男" },
              { checked: true, anme: "女", value: "女" }]
            })
            break;
        }


      },
      fail(res) {
        // console.log("识别失败", res)
      }
    })
    // console.log("识别成功res.result.name")


  },
  select_person: function () {
    const db = wx.cloud.database();
    db.collection('info_zmyy').where({
      phone: select_value,
      form: "住院分诊台"
    }).limit(1)
      .orderBy('gzry_addtime', 'desc')//按字段排序
      .get()
      .then(res => {


            if (res.data.length) {
              wx.showToast({
                title: '信息已填充',
                icon: 'success',
              })
              frist_on = "再次登记人员"
              twvalue = ""
              this.setData({
                name: res.data[0].name,
                sfz: res.data[0].sfz,
                phone: res.data[0].phone,
                gzry_memo_zy: res.data[0].gzry_memo_zy,
                laifangmudi: res.data[0].laifangmudi,
                beifangzhekeshi: res.data[0].beifangzhekeshi,
                beifangzhename: res.data[0].beifangzhename,
              })
             
              
            }  
            else {
              wx.showToast({
                title: '无就诊记录',
                icon: 'success',
              })
              twvalue = ""
              this.setData({
                phone: select_value
              })
            }
            
           
          })

 
    


  },
  //确定按钮点击事件,门诊体温
  zmyymz_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_mz_ms();
    this.setData({
      zmyymz_fz_tw_moda: true,
    })
  },
  //取消按钮点击事件,门诊体温
  zmyymz_tw_ca: function () {
   
    this.setData({
      zmyymz_fz_tw_moda: true,
    })
  }, 
 
  findOpenid: function () {
    var that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              gzry_dep = res.data[0].dep
              gzry_zqdd = res.data[0].zqaddress
              gzry_name = res.data[0].name
              gzryphone = res.data[0].gzryphone
            }
            else {
               wx.showToast({
                title: '你是怎么来进的'
              })

            }
          })
      }
    })


  },
  zmyymz_twvalue_check: function (e) {
    if (parseFloat(twvalue) > 34 && parseFloat(twvalue) < 41) {
      // console.log("输出1：", parseFloat(twvalue))
      if (parseFloat(twvalue) >= 37) {
        console.log("体温高", parseFloat(twvalue))
        this.setData({
          zmyymz_fz_tw_moda: false,
        })
      }
      else {
        console.log("体温正常", parseFloat(twvalue))
        this.add_mz_ms();

      }
    }
    else {
      console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    }
  },
  add_mz_ms :function() {
    const db = wx.cloud.database()
    db.collection('info_zmyy').add({
      data: {
        name: name,
        sfz: sfz,
        phone: phone, 
        laifangmudi: laifangmudi,
        beifangzhekeshi: beifangzhekeshi,
        beifangzhename: beifangzhename,      
        gzry_memo: gzry_memo_zy,
        form: "住院分诊台",
        input: "工作人员录入",
        gzryphone: gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address: gzry_zqdd,
        gzry_dep: gzry_dep,
        gzry_name: gzry_name,
        twvalue: twvalue,
        outtime: "",
        frist_on: frist_on

       
      }
      ,
      success: res => {
        // 在返回结果中会包含新创建的记录的 _id                  
        // wx.showToast({
        //   title: '新增记录成功',
        // })
        app.globalData.gzry_input_name = name
        app.globalData.gzry_input_twvalue = twvalue
        console.log('添加数据02', res)
        wx.switchTab({
          url: '/pages/info/hospital/hospital'
        })

      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '新增记录失败,请找其他人员登记！'
        })
        // console.error('[数据库] [新增记录] 失败：', err)
      }
    })
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
   
    if (e.detail.value.name_mz && e.detail.value.sfzh &&
      e.detail.value.laifangmudi && e.detail.value.beifangzhekeshi && e.detail.value.beifangzhename) {
      
       
        name= e.detail.value.name_mz,
            sfz= e.detail.value.sfzh,
            phone= e.detail.value.phone,
            laifangmudi= e.detail.value.laifangmudi,
            beifangzhekeshi= e.detail.value.beifangzhekeshi,
            beifangzhename= e.detail.value.beifangzhename,
          gzry_memo_zy = e.detail.value.gzry_memo_zy
      this.zmyymz_twvalue_check(); 
     
    }
    else {
      // console.log('form发生了submit事件，携带数据为：', e.detail.value)
      wx.showToast({
        icon: 'loading',
        title: '请填全红色信息',
      })
     
    }
  
  },
  onInputEvent: function (e) {
    twvalue = e.detail.value
    // this.setData({
    //   returetwvalue: "  体温：" + twvalue
    // })
  },
  select_value: function (e) {
    select_value = e.detail.value
    console.log('输入值：', select_value)
  },
})